package com.wandian.view.mediator
{
	import com.wandian.controller.CLZNoticeName;
	import com.wandian.data.GameData;
	import com.wandian.model.socket.net.Command;
	import com.wandian.model.vo.RoomVo;
	import com.wandian.util.CLZButton;
	import com.wandian.util.CLZButtonFour;
	import com.wandian.util.CLZLlistScrollPanel;
	import com.wandian.util.CLZLoadingData;
	import com.wandian.util.CLZTabBar;
	import com.wandian.util.CLZTiming;
	import com.wandian.util.RadioRoom;
	import com.wandian.view.components.ui.RoomInfo;

	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;

	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;

	/**
	 * 房间列表中介者
	 * @author long
	 */
	public class RoomListMediator extends Mediator
	{
		private var _global:GameData;
		private var _mcConten:Sprite;
		private var _minTabBar:CLZTabBar;
		private var _arrShowRoom:Array;
		private var _recordNum:int;
		private var _radioManage:RadioRoom;
		private var _arrListData:Array;
		private const MIN_TO_MAX:int=0;
		private const MAX_TO_MIN:int=1;
		private var _nCurSortMinToMax:int=0;
		public static const NAME:String="RoomListMediator";

		public function RoomListMediator(viewComponent:Object=null, arrListData:Array=null)
		{
			super(NAME, viewComponent);
			this._global=GameData.getInstance();
			this._mcConten=new Sprite();
			this._mcConten.name="RoomListMediator_mcConten";
			this._arrListData=arrListData;
		}

		override public function onRegister():void
		{
			this.roomList.addChild(this._mcConten);
			this._mcConten.x=14;
			this._mcConten.y=74.5;
			this.removeAllInfo();
			this.roomList["radio1Txt"].text="隐藏满房";
			this.roomList["radio2Txt"].text="隐藏空房";
			CLZButton.setButton(this.roomList.id_btn, "编号");
			CLZButton.setButton(this.roomList.name_btn, "房间名称");
			CLZButton.setButton(this.roomList.type_btn, "房间类型");
			CLZButton.setButton(this.roomList.player_btn, GameData.tip["ROOM_PLAYER_NAME"]);
			CLZButton.setButton(this.roomList.blind_btn, GameData.tip["ROOM_BLIND_NAME"]);
			CLZButton.setButton(this.roomList.min_btn, GameData.tip["ROOM_MIN_NAME"]);
			CLZButton.setButton(this.roomList.max_btn, GameData.tip["ROOM_MAX_NAME"]);
			this.roomList.id_btn.addEventListener(MouseEvent.CLICK, this.onSortHandler);
			this.roomList.name_btn.addEventListener(MouseEvent.CLICK, this.onSortHandler);
			this.roomList.type_btn.addEventListener(MouseEvent.CLICK, this.onSortHandler);
			this.roomList.player_btn.addEventListener(MouseEvent.CLICK, this.onSortHandler);
			this.roomList.blind_btn.addEventListener(MouseEvent.CLICK, this.onSortHandler);
			this.roomList.min_btn.addEventListener(MouseEvent.CLICK, this.onSortHandler);
			this.roomList.max_btn.addEventListener(MouseEvent.CLICK, this.onSortHandler);
			CLZButton.setButton(this.roomList.refresh_btn, GameData.tip["REFRESH"]);
			CLZButton.setButton(this.roomList.fastStartGame_btn, GameData.tip["FAST_START_GAME"]);
			CLZButtonFour.setButton(this.roomList.startGame_btn, GameData.tip["ENTER_GAME"], false, "0xffffff,0xffffff,0xffffff,0xffffff", false);
			this._radioManage=new RadioRoom([this.roomList.radio1, this.roomList.radio2]);
			this._radioManage.addEventListener(Event.CHANGE, this.changeRadioHandler);
			var tabs:Array=[[this.roomList.btnTab1, GameData.tip["ALL_TABBLE"], true, "0xCCCCCC,0xcccccc,0xffffff"], [this.roomList.btnTab2, GameData.tip["NOVICE_TABBLE"], true, "0xCCCCCC,0xcccccc,0xffffff"], [this.roomList.btnTab3, GameData.tip["PRIMARY_TABBLE"], true, "0xCCCCCC,0xcccccc,0xffffff"], [this.roomList.btnTab4, GameData.tip["INTERMEDIATE"], true, "0xCCCCCC,0xcccccc,0xffffff"], [this.roomList.btnTab5, GameData.tip["SENIOR_TABBLE"], true, "0xCCCCCC,0xcccccc,0xffffff"], [this.roomList.btnTab6, GameData.tip["FRIERCE_TABBLE"], true, "0xCCCCCC,0xcccccc,0xffffff"]];
			this._minTabBar=new CLZTabBar(tabs);
			var total:*=tabs.length;
			var count:int=0;
			while (count < total)
			{

				tabs[count][0].addEventListener(Event.CHANGE, this.onChangeMinTabBarHandler);
				count=count + 1;
			}
			if (this._global.userInfo.userVo.money >= 20000000)
			{
				this._minTabBar.setClickIndex(this.roomList["btnTab6"]);
			}
			else if (this._global.userInfo.userVo.money >= 2000000)
			{
				this._minTabBar.setClickIndex(this.roomList["btnTab5"]);
			}
			else if (this._global.userInfo.userVo.money >= 250000)
			{
				this._minTabBar.setClickIndex(this.roomList["btnTab4"]);
			}
			else if (this._global.userInfo.userVo.money >= 100)
			{
				this._minTabBar.setClickIndex(this.roomList["btnTab3"]);
			}
			else
			{
				this._minTabBar.setClickIndex(this.roomList["btnTab2"]);
			}
			this.roomList.fastStartGame_btn.addEventListener(MouseEvent.CLICK, this.fastStartGameHandler);
			this.roomList.startGame_btn.addEventListener(MouseEvent.CLICK, this.startGameHandler);
			this.roomList.refresh_btn.addEventListener(MouseEvent.CLICK, this.onRefreshHandler);
			//请求房间，同时loading
			CLZLoadingData.show(this._mcConten, 658, 263);
//			facade.sendNotification(CLZNoticeName.SOCKETCOMMAND_ROOM_LIST_REQ);

			if (this._arrListData != null)
			{
				trace("OnReg从大厅获取房间列表成功!");
				CLZLoadingData.hide(this._mcConten);
				var arrCheckRoom:Array=this.checkNullAndFull(this._arrListData);
				this.selectedShowRoomData(arrCheckRoom);
			}
			else
			{
				trace("OnReg从大厅获取房间列表失败!");
			}
			return;
		}

		override public function listNotificationInterests():Array
		{
			return [CLZNoticeName.UPDATEROOMPLAYERNUMCHANGE, CLZNoticeName.LOBBY_UPDATEROOMVO, CLZNoticeName.ROOMLISTMEDIATOR_CMDRES_ROOMLIST, CLZNoticeName.GET_ROOM_LIST, CLZNoticeName.ROOM_LIST_INFO_SUCC, CLZNoticeName.FAST_ENTER_GAME_RES, CLZNoticeName.OUT_GAME_ROOM_RSP];
		}

		private function updateRoomVo(arrInfo:Array):void
		{
			if (arrInfo == null || arrInfo.length < 8)
				return;
			var roomVo:RoomVo;
			trace("[lobbymediator]收到更新roomvo广播，房间:" + arrInfo[0]);
			for (var i:int=0; i < this._arrShowRoom.length; i++)
			{
				roomVo=this._arrShowRoom[i].roomVo;
				if (roomVo.id == arrInfo[0])
				{
					roomVo.name=arrInfo[1];
					roomVo.seat=arrInfo[2];
					roomVo.minBlind=arrInfo[3];
					roomVo.maxblind=arrInfo[4];
					roomVo.minStake=arrInfo[5];
					roomVo.maxStake=arrInfo[6];
					roomVo.playerNum=arrInfo[7];
					(this._arrShowRoom[i] as RoomInfo).setInfo();
				}
			}
		}

		private function updatePlayerNum(arrInfo:Array):void
		{
			if (arrInfo == null || arrInfo.length < 2)
				return;
			var roomVo:RoomVo;
			trace("[RoomListMediator]收到更新房间人变化广播，房间:" + arrInfo[0]);
			for (var i:int=0; i < this._arrShowRoom.length; i++)
			{
				roomVo=this._arrShowRoom[i].roomVo;
				if (roomVo.id == arrInfo[0])
				{
					roomVo.playerNum=arrInfo[1];
					(this._arrShowRoom[i] as RoomInfo).setPlayerNum();
				}
			}
		}

		override public function handleNotification(notification:INotification):void
		{
			var arrCheckRoom:Array=null;
			var data:Object=null;
			switch (notification.getName())
			{
				case CLZNoticeName.LOBBY_UPDATEROOMVO:
				{
					updateRoomVo(notification.getBody() as Array);
					break;
				}
				case CLZNoticeName.UPDATEROOMPLAYERNUMCHANGE:
				{
					updatePlayerNum(notification.getBody() as Array);
					break;
				}

				case CLZNoticeName.ROOMLISTMEDIATOR_CMDRES_ROOMLIST: //服务器响应房间列表
				{
					CLZLoadingData.hide(this._mcConten);
					this._arrListData=notification.getBody() as Array;
					arrCheckRoom=this.checkNullAndFull(this._arrListData);
					GameData.arrAllRoom=this._arrListData;
					this.selectedShowRoomData(arrCheckRoom);
					break;
				}
				case CLZNoticeName.GET_ROOM_LIST:
				{
					CLZLoadingData.hide(this._mcConten);
//					this._arrListData = notification.getBody() as Array;
					GameData.arrAllRoom=this._arrListData;
					arrCheckRoom=this.checkNullAndFull(this._arrListData);
					this.selectedShowRoomData(arrCheckRoom);
					break;
				}
				case CLZNoticeName.ROOM_LIST_INFO_SUCC:
				{
					CLZLoadingData.hide(this._mcConten);
					this._arrListData=notification.getBody() as Array;
					arrCheckRoom=this.checkNullAndFull(this._arrListData);
					this.selectedShowRoomData(arrCheckRoom);
					break;
				}
				case CLZNoticeName.FAST_ENTER_GAME_RES:
				{
					this.sendNotification(CLZNoticeName.ENTER_GAME_ROOM_REQ, notification.getBody() as RoomVo);
					break;
				}
				case CLZNoticeName.DOUBLE_PALYER_ON_ROOM:
				{
					this.sendNotification(CLZNoticeName.ENTER_GAME_ROOM_REQ, this._arrShowRoom[this._recordNum].roomVo);
					break;
				}
				case CLZNoticeName.OUT_GAME_ROOM_RSP: //初始化开始按钮，请求最新的房间列表
				{
					data=notification.getBody();
					if (this._global.userInfo.userVo.userid == data.userid)
					{
						CLZButtonFour.initBtn(this.roomList.startGame_btn, false);
						this.onRefreshHandler(null);
						this.sendNotification(CLZNoticeName.UPDATE_FRIEND_STATE); //更新好友状态
					}
					break;
				}
				default:
				{
					break;
				}
			}
			return;
		}

		private function onChangeMinTabBarHandler(event:Event):void
		{
			switch (event.target.name)
			{
				case "btnTab1":
				{
					this._global.minTableType=GameData.ALL;
					break;
				}
				case "btnTab2":
				{
					this._global.minTableType=GameData.P1;
					break;
				}
				case "btnTab3":
				{
					this._global.minTableType=GameData.G0;
					break;
				}
				case "btnTab4":
				{
					this._global.minTableType=GameData.G1;
					break;
				}
				case "btnTab5":
				{
					this._global.minTableType=GameData.G2;
					break;
				}
				case "btnTab6":
				{
					this._global.minTableType=GameData.G3;
					break;
				}
				default:
				{
					break;
				}
			}
			CLZButtonFour.initBtn(this.roomList.startGame_btn, false);
			CLZLoadingData.show(this._mcConten, 658, 263);
			facade.sendNotification(CLZNoticeName.GET_ROOM_LIST);
			this.sendNotification(CLZNoticeName.UPDATE_FRIEND_STATE);
			return;
		}

		private function fastStartGameHandler(event:MouseEvent):void
		{
//			this.sendNotification(CLZNoticeName.FAST_ENTER_GAME_REQ);
			this.sendNotification(CLZNoticeName.FAST_ENTER_GAME_REQ);
			return;
		}

		private function startGameHandler(event:MouseEvent):void
		{
			var roomInfo:RoomInfo=this._arrShowRoom[this._recordNum];
			if (roomInfo != null)
			{
				this.sendNotification(CLZNoticeName.ENTER_GAME_ROOM_REQ, roomInfo.roomVo);
			}
			else
			{
				this.sendNotification(CLZNoticeName.FAST_ENTER_GAME_REQ);
			}
			return;
		}

		private function onRefreshHandler(event:MouseEvent):void
		{
			this.removeAllInfo();
			facade.sendNotification(CLZNoticeName.GET_ROOM_LIST);
			return;
		}

		private function changeRadioHandler(event:Event):void
		{
			var arrCheck:*=this.checkNullAndFull(this._arrListData);
			this.selectedShowRoomData(arrCheck);
			return;
		}

		private function checkNullAndFull(arrRoomVo:Array):Array
		{
			var count:int=0;
			var temp:RoomVo=null;
			if (arrRoomVo == null)
			{
				return [];
			}
			var arrCheck:Array=[];
			var total:*=arrRoomVo.length;
			switch (this._radioManage._radioId)
			{
				case GameData.RADIO1:
				{
					count=0;
					while (count < total)
					{

						temp=arrRoomVo[count] as RoomVo;
						if (temp.seat != temp.playerNum)
						{
							arrCheck.push(temp);
						}
						count=count + 1;
					}
					break;
				}
				case GameData.RADIO2:
				{
					count=0;
					while (count < total)
					{

						temp=arrRoomVo[count] as RoomVo;
						if (temp.playerNum != 0)
						{
							arrCheck.push(temp);
						}
						count=count + 1;
					}
					break;
				}
				case GameData.RADIO3:
				{
					count=0;
					while (count < total)
					{

						temp=arrRoomVo[count] as RoomVo;
						if (temp.seat != temp.playerNum)
						{
						}
						if (temp.playerNum != 0)
						{
							arrCheck.push(temp);
						}
						count=count + 1;
					}
					break;
				}
				default:
				{
					arrCheck=arrRoomVo;
					break;
				}
			}
			return arrCheck;
		}

		private function onSortHandler(event:MouseEvent):void
		{
			var id:String=null;
			switch (event.target.name)
			{
				case "id_btn":
				{
					id="id";
					break;
				}
				case "name_btn":
				{
					id="name";
					break;
				}
				case "type_btn":
				{
					id="type";
					break;
				}
				case "player_btn":
				{
					id="playerNum";
					break;
				}
				case "blind_btn":
				{
					id="blind";
					break;
				}
				case "min_btn":
				{
					id="min";
					break;
				}
				case "max_btn":
				{
					id="max";
					break;
				}
				default:
				{
					break;
				}
			}
			var arrRoom:Array=[];
			if (this._nCurSortMinToMax == this.MIN_TO_MAX)
			{
				this._nCurSortMinToMax=this.MAX_TO_MIN;
				arrRoom=GameData.sortMaxToMinArray(this._arrListData, id);
			}
			else
			{
				this._nCurSortMinToMax=this.MIN_TO_MAX;
				arrRoom=GameData.sortMinToMaxArray(this._arrListData, id);
			}
			arrRoom=this.checkNullAndFull(arrRoom);
			this.showRoomList(arrRoom);
			return;
		}

		private function showRoomList(arrRoom:Array):void
		{
			var roomMc:MovieClip=null;
			this.removeAllInfo();
			var roomPanel:CLZLlistScrollPanel;
			if (GameData.is800)
			{
				roomPanel=new CLZLlistScrollPanel(1, 550, 26, 0, 0, 263, 546);
			}
			else
			{
				roomPanel=new CLZLlistScrollPanel(1, 658, 26, 0, 0, 263, 658);
			}
			this._mcConten.addChild(roomPanel);
			this._arrShowRoom=[];
			var total:*=arrRoom.length;
			var count:int=0;
			while (count < total)
			{

				roomMc=GameData.getMovieClip("RoomInfo");
				roomPanel.setMc(roomMc, count, total);
				this._arrShowRoom[count]=new RoomInfo(roomMc, arrRoom[count], count);
				this._arrShowRoom[count].addEventListener(Event.CHANGE, this.downRoomHandler);
				count=count + 1;
			}
			return;
		}

		private function selectedShowRoomData(arrCheckRoom:Array):void
		{
			var tempRoom:RoomVo=null;
			var arrResult:Array=[];
			var total:*=arrCheckRoom.length;
			var arrCloseRoom:Array=[];
			var arrNormal:Array=[];
			GameData.sortMinToMaxArray(arrCheckRoom, "min");
			var count:int=0;
			while (count < total)
			{

				tempRoom=this._global.constRoomVoEnter(arrCheckRoom[count]) as RoomVo;
				if (tempRoom.close)
				{
					if (tempRoom.playerNum > 0)
					{
						arrCloseRoom.push(tempRoom);
					}
					else
					{
						arrNormal.push(tempRoom);
					}
				}
				else if (tempRoom.playerNum > 0)
				{
					arrCloseRoom.unshift(tempRoom);
				}
				else
				{
					arrNormal.unshift(tempRoom);
				}
				count=count + 1;
			}
			arrResult=arrCloseRoom.concat(arrNormal);
			this.showRoomList(arrResult);
			return;
		}

		private function downRoomHandler(event:Event):void
		{
			var roomInfo:RoomInfo=null;
			if (this._recordNum != event.target.num)
			{
				roomInfo=this._arrShowRoom[this._recordNum];
				if (roomInfo != null)
				{
					roomInfo.init();
				}
			}
			this._recordNum=event.target.num;
			var roomVo:*=event.target.roomVo as RoomVo;
			if (roomVo != null)
			{
				CLZButtonFour.initBtn(this.roomList.startGame_btn, true);
			}
			//获取房间列表信息
			CLZTiming.setTimingNotice(1000, CLZNoticeName.ON_CHANGE_ROOM_ID, this._arrShowRoom[this._recordNum].roomVo.id);
			return;
		}

		private function removeAllInfo():void
		{
			while (this._mcConten.numChildren)
			{

				this._mcConten.removeChildAt(0);
			}
			return;
		}

		private function get roomList():MovieClip
		{
			return viewComponent as MovieClip;
		}
	}
}
